package com.simple.datax.intra.service.impl;

import java.util.List;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.rpc.RpcException;
import com.simple.datax.SimpleMessage;
import com.simple.datax.api.CommonSimpleException;
import com.simple.datax.api.MessageProcessor;
import com.simple.datax.api.NeedResendSimpleException;
import com.simple.datax.api.Sender;
import com.simple.datax.api.SimpleException;
import com.simple.datax.service.AbstractMessageProcessor;

/**
 * @Title: CallSenderServiceImpl.java
 * @Package com.simple.datax.intra.service.impl
 * @Description: 调用Sender的服务,因为要区分不同平台而加入的了两个sender的调用
 * @author Paladin Chen
 * @date 2013-1-7 下午2:28:02
 * @version V1.0
 */
public class CallSenderServiceImpl extends AbstractMessageProcessor implements MessageProcessor {
	private static final Logger logger = LoggerFactory.getLogger(CallSenderServiceImpl.class);

	private Sender winSender;

	private Sender senders;

	private List<String> winNames;

	@Override
	public void realProcess(SimpleMessage src) throws SimpleException {
		logger.debug("Begin to Call Sender Service!" + src.toLog());

		try {
			String communType = (src).getSentCommunType().toUpperCase();

			// 发送前去除内部Doc对象
			src.setContent(src.getContent());
			src.setIntraDoc(null);

			if (winNames != null && winNames.contains(communType)) {
				winSender.send(src);
			} else {
				senders.send(src);
			}
		} catch (RpcException e) {
           throw new NeedResendSimpleException("Call Sender:", e);
		} catch (Exception e) {
			throw new CommonSimpleException("Call Sender Error:" + e.getMessage(), e);
		}

		// 打印报文
		callNextStep(src);

		logger.debug("Finish Call Sender Service!" + src.toLog());
	}

	public Sender getWinSender() {
		return winSender;
	}

	public void setWinSender(Sender winSender) {
		this.winSender = winSender;
	}

	public Sender getSenders() {
		return senders;
	}

	public void setSenders(Sender senders) {
		this.senders = senders;
	}

	public List<String> getWinNames() {
		return winNames;
	}

	public void setWinNames(List<String> winNames) {
		this.winNames = winNames;
	}
}
